﻿using System;
using System.Collections;
using System.Data.SqlClient;
using com.fukefu.report;
using com.fukefu.sql;
using com.fukefu.tools;

namespace com.fukefu.template
{
    public partial class TemplateMainForm : BaseForm
    {
        string mid = null;
        Hashtable k_type = new Hashtable();

        ArticleTag tag = new ArticleTag();
        SqlServerOperate sql = new SqlServerOperate();

        public TemplateMainForm()
            : this(null)
        {

        }
        public TemplateMainForm(string mid)
        {
            InitializeComponent();
            Init();
            if (mid != null)
            {
                this.mid = mid;
                LoadHtml();
                this.Text = "编辑-模板制作";
            }
        }
        private void Init()
        {
            SqlDataReader re = sql.Query("select ktid,word from 全局设置.dbo.[_bt_关键字分类] order by weights desc");
            while (re.Read())
            {
                k_type.Add(re["word"], re["ktid"]);
                TypeToolStripComboBox.Items.Add(re["word"]);
            }
            re.Close();
            //生成组合
            NewTitleTemp();
        }
        //生成头尾部
        private void NewTitleTemp()
        {
            int id = MyFunction.GetRandom(100);
            string html = "";
            SqlDataReader re = sql.Query("select * from [全局设置].dbo.[_mb_标签组合] where " + id + ">=minnum and " + id + "<maxnum");
            if (re.Read())
            {
                html = tag.ReplaceAllTag(re["cont"].ToString());
            }
            re.Close();
            htb.SetHtml(html);
        }
        //加载制定id文档
        private void LoadHtml()
        {
            SqlDataReader re = sql.Query("select cont,word from [_mb_模板] as a,[全局设置].dbo.[_bt_关键字分类] as b where id=" + mid + " and a.ktid=b.ktid");
            if (re.Read())
            {
                htb.SetHtml(re["cont"].ToString());
                TypeToolStripComboBox.Text = re["word"].ToString();
            }
            re.Close();
        }
        //新建模板
        private void New_Click(object sender, EventArgs e)
        {
            if (ShowCheckDialog("确定要新建模板吗？"))
            {
                mid = null;
                NewTitleTemp();
                this.Text = "新建-模板制作";
            }
        }
        //保存
        private void Save_Click(object sender, EventArgs e)
        {
            if (TypeToolStripComboBox.Text.Equals(""))
            {
                ShowMessageDialog("请选择分类!");
                return;
            }
            if (ShowCheckDialog("确定保存吗？"))
            {
                string html = htb.GetHtml();
                html = html.Replace("'", "''");
                if (mid == null)
                {
                    sql.Update("insert into [_mb_模板](cont,uid,ktid) values('" + html + "','" + LoginUser.Uid + "','" + k_type[TypeToolStripComboBox.Text] + "')");
                    mid = sql.GetMaxId("select max(id) from [_mb_模板] where uid='" + LoginUser.Uid + "'").ToString();
                }
                else
                {
                    sql.Update("update [_mb_模板] set cont='" + html + "' where id='" + mid + "'");
                }
                this.Text = "已保存-模板制作";
            }
        }
        //发布数量统计
        private void Statistics_Click(object sender, EventArgs e)
        {
            string cmd = "select 全局设置.dbo.[_bt_关键字分类].word as 名称,COUNT(1) as 数量 from _mb_模板,全局设置.dbo.[_bt_关键字分类] where _mb_模板.ktid=全局设置.dbo.[_bt_关键字分类].ktid  and DATEDIFF(D,GETDATE(),dt)=0 and uid='" + LoginUser.Uid + "' group by 全局设置.dbo.[_bt_关键字分类].word";
            new StatisticsForm("今日模板录入统计", cmd).Show();
        }
        //切换头尾部
        private void QH_Click(object sender, EventArgs e)
        {
            NewTitleTemp();
        }
        //预览
        private void Preview_Click(object sender, EventArgs e)
        {
            ShowForm(PreviewForm.GetInstall(htb.GetHtml()));
        }
        //查看列表
        private void ListToolStripButton_Click(object sender, EventArgs e)
        {
            ShowForm(ListForm.GetInstance("模板"));
        }

        private void FlushToolStripButton_Click(object sender, EventArgs e)
        {
            NewTitleTemp();
        }
    }
}
